package com.zsix.system.strategy.jump;

import com.zsix.system.mapper.taskAssigneeMapper.ApprovalTrackMapper;
import com.zsix.system.service.manager.ProcessTaskServiceManager;
import com.zsix.common.dto.SendActionDto;
import com.zsix.common.dto.controllerParams.ProcessTaskParams;
import com.zsix.common.entity.taskAssignee.ApprovalTrack;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JumpAppeared implements JumpStrategy {
    @Autowired //任务服务
    private TaskService taskService;
    @Autowired
    private ProcessTaskServiceManager processTaskServiceManager;
    @Autowired
    private ApprovalTrackMapper approvalTrackMapper;
    /**
     * 如果与上一步处理人相同，执行跳转
     * @param username
     * @param processTaskParams
     * @param toUsers
     * @return
     */
    @Override
    public SendActionDto Do(String username, ProcessTaskParams processTaskParams,
                            String toUsers){
        SendActionDto sendActionDto=new SendActionDto();
        List<ApprovalTrack> approvalTracks=approvalTrackMapper
                .approvalTracks(processTaskParams.getProcessInstanceId());
        Boolean isHaveUser=false;
        for(ApprovalTrack approvalTrack:approvalTracks){
            if(approvalTrack.getUserName().equals(username))
            {
                isHaveUser=true;
                break;
            }
        }
        if(isHaveUser) {
            //先拾取任务
            taskService.setAssignee(processTaskParams.getTaskId(), username);
            sendActionDto = processTaskServiceManager.sendWork(processTaskParams, username);
        }
        return sendActionDto;
    }
    /**
     * 返回服务类型
     * @return
     */
    @Override
    public String getType(){
        return "appeared";
    }
}
